package com.css.fxfzaqbz.modules.assess1.repository.impl;

import com.css.fxfzaqbz.modules.assess1.entity.AlgEntity;
import com.css.fxfzaqbz.modules.assess1.entity.AlgVO;
import com.css.fxfzaqbz.modules.assess1.entity.DistrictEntity;
import com.css.fxfzaqbz.modules.assess1.repository.DestoryAlgRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-12-06 14:42
 */
@Repository
public class DestoryAlgRepositoryImpl implements DestoryAlgRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void save(AlgEntity algEntity) {
        String sql = "insert into sys_destory_alg  values(:id, :province, :city, :prefecture, " +
                ":deathNum, :injuryNum, :levell, :iaId,:resettle,:deathXznum,:injuryXznum,:resettleXz,ST_GeometryFromText(:geom,4490),:resultAnalysis,:number,:deathAndInjuryNum)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(algEntity);
        namedParameterJdbcTemplate.update(sql, source);
    }

    @Override
    public void delete(DistrictEntity districtEntity, String iaId) {

        String sql="delete from sys_destory_alg where province='"+districtEntity.getProvince()+"' and city='"+districtEntity.getCity()+"' and prefecture='"+districtEntity.getPrefecture()+"' and ia_id='"+iaId+"' ";
        //String sql = "delete from sys_destory_alg ";
        jdbcTemplate.update(sql);
    }

    @Override
    public AlgEntity getAlg(String province, String city, String prefecture, AlgVO algVO) {
        String sql = "select *,ST_AsText(geom) geom from sys_destory_alg where province='" + province + "' and  city='" + city + "' and  prefecture='" + prefecture + "' and ia_id='" + algVO.getIaId() + "' ";
        AlgEntity algEntity = null;
        try {
            algEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(AlgEntity.class));
            return algEntity;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void update(String id, int intValue) {
        String sql = "update sys_destory_alg set resettle ='" + intValue + "' ,resettle_xz='" + intValue + "' where id='" + id + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void updateAlg(AlgEntity algEntity) {
        Integer deathAndInjuryNum=algEntity.getDeathXznum()+algEntity.getInjuryXznum();
        String sql = "update sys_destory_alg set levell='" + algEntity.getLevell() + "', death_xznum =" + algEntity.getDeathXznum() + ",injury_xznum=" + algEntity.getInjuryXznum() + ",resettle_xz=" + algEntity.getResettleXz() + ", death_and_injury_num="+deathAndInjuryNum+" where id='" + algEntity.getId() + "'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<AlgEntity> getAlgByIaId(String iaId) {
        String sql = "select *,ST_AsText(geom) geom from sys_destory_alg where  ia_id='" + iaId + "' ";
        try {
            //改
            List<AlgEntity> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AlgEntity.class));
            return query;
        } catch (Exception e) {
            return null;
        }

    }

    @Override
    public String getGeomBySSQ(String code) {
        String sql = "select ST_AsText(geom)  from district_boundary_county where code ='" + code + "' ;";
        String s = null;
        try {
            s = jdbcTemplate.queryForObject(sql, String.class);
            return s;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public List<AlgEntity> getList(String id) {
        String sql = "select *,ST_AsText(geom) geom from sys_destory_alg where  result_analysis='" + id + "' ";
        List<AlgEntity> query = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(AlgEntity.class));
        return query;
    }
}
